package com.google.code.virtualhockey.vhx.shared;

import com.extjs.gxt.ui.client.util.IconHelper;
import com.google.code.virtualhockey.vhx.shared.rpc.IRpcCommandService;
import com.google.code.virtualhockey.vhx.shared.rpc.IRpcCommandServiceAsync;
import com.google.code.virtualhockey.vhx.shared.rpc.RpcCommandDispatcher;
import com.google.code.virtualhockey.vhx.shared.rpc.helper.GaeSecurityTokenProvider;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * Contains static utilities, gives access to resources and serves as container for
 * constants.
 */
public final class VhxSharedUtils
{
  public static final String                 URL_WELCOME_PAGE    = "/index.html";
  public static final String                 SESSION_COOKIE      = "ACSID";
  public static final AbstractImagePrototype ICON_LOGIN          = icon16( "images/icons/login16.png" );
  public static final AbstractImagePrototype ICON_LOGOUT         = icon16( "images/icons/logout16.png" );

  private static final String                WEB_MODE_URL_SUFFIX = "";
  private static final String                DEV_MODE_URL_SUFFIX = "%3Fgwt.codesvr=127.0.0.1:9997";


  private static RpcCommandDispatcher        d_dispatcher;

  /**
   * Returns the central command dispatcher.
   */
  public static RpcCommandDispatcher dispatcher()
  {
    if ( d_dispatcher == null )
      d_dispatcher = new RpcCommandDispatcher( ( IRpcCommandServiceAsync) GWT.create( IRpcCommandService.class ), new GaeSecurityTokenProvider() );

    return d_dispatcher;
  }

  /**
   * Returns the URL suffix containing the address of the GWT code server if running in
   * dev mode or an empty string otherwise.
   */
  public static String getUrlSuffix()
  {
    if ( GWT.isScript() )
      return WEB_MODE_URL_SUFFIX;
    else
      return DEV_MODE_URL_SUFFIX;
  }

  /**
   * Creates an icon based on the given URL in 16 by 16 pixel size.
   *
   * @param strUrl the URL of the icon to be created.
   * @return the created icon
   */
  public static AbstractImagePrototype icon16( String strUrl )
  {
    return IconHelper.createPath( strUrl, 16, 16 );
  }

  /** Private constructor to avoid instantiation. */
  private VhxSharedUtils()
  {}
}
